--
-- Author: bin.zhao
-- Date: 2018-7-7
--
-- PartnerNewView  获得新随从界面

local AvatarFactory = app:getClass("AvatarFactory")
local Armature = app:getClass("Armature")

local fileName = string.getModeName(...)
local ClassRef = class(fileName, app.mvc.ViewBase)

ClassRef.VIEW_ROOT_RES = Res.MountNewScene

local PARTNER_GET_STR = L("lua_code_text_471")

function ClassRef:init()
    self._ui["Panel/Panel_bg/Image_1"]:setContentSize(display.size)

    self._ui.Button_1:addClickEventListener(function()
        self._ui.Button_1:hide()
        self._ui.Button_2:hide()
        local pc = app:getInst("PhotoController")
        pc:snapshotNode(self)
        self._ui.Button_1:stopAllActions()
        local act = cca.seq({cca.delay(0), cca.callFunc(function()
            --UIHandler:getPopupUi():removeUIByKey(ClassRef:getViewName())
            pc:showPhotoViewShare()
            self._ui.Button_1:show()
            self._ui.Button_2:show()
        end), })

        self._ui.Button_1:runAction(act)
    end)
    self._ui.Button_2:addClickEventListener(function()
        executeFunc(self._onCloseView, self)
    end)

    self._starTimeline = self._ui["Panel/light"]:getTimeline()
    self._starTimeline:pause()
end

function ClassRef:testGetPartnerByID(id)
    local configs = GD:queryPartners()
    local haveNum = 0
    local parter = {}
    local userCfg = UD:getPartnerByTid(id) 
    if userCfg then
        haveNum = haveNum+1
        parter.bHave = true
        parter.userCfg = userCfg
    else
        parter.bHave = false
    end
    return parter
end

function ClassRef:show()
    self:setVisible(true)
    self._root:setVisible(false)

    local params = {
        armatureName = "getride",
        path = Res.MountNewEfcPath
    }
    local armature = Armature.create(params)
    armature:setPosition(display.cx, display.cy)
    self:addChild(armature, 2)

    armature:play("getride", 1, function(sender)
            sender:removeFromParent()
        end)
    armature:onFrameEvent(function(sender, name)
            if name == "all" then
                self._root:setVisible(true)
                local quality = self._data:getQuality()
                quality = tonumber(quality)
               
                local armatureNode = AvatarFactory.createPartnerByConf(self._data:getShowAvatorParams())
                armatureNode:setName("arm_node")
                local defautAction = "mapidle"
                if armatureNode:hasAnimation(defautAction) then
                    armatureNode:getAnimation():play(defautAction)
                end
                self._ui.Node_Partner:addChild(armatureNode)
            end
        end)
    AM.playSoundById(5153)
end

function ClassRef:refresh(partner)
    if partner == nil then return end
    self._data = partner

    local name = partner:getName()
    self._ui.title:setString(string.format(PARTNER_GET_STR, name))
end

return ClassRef